﻿using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Web.Configuration;

namespace msBsc.AppLogic.DataAlc
{
    /// <summary>
    /// 自选路线基本信息表
    /// </summary>
    public class CBizDjkOrderRoute : CustBizObject
    {
        private CDmDjkOrderRoute m_CDmDjkOrderRoute = new CDmDjkOrderRoute();
        private CDmDjkOrderRoute oDmDjkOrderRoute
        {
            get { return m_CDmDjkOrderRoute; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizDjkOrderRoute()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkOrderRoute.DJK_ORDER_ROUTE);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["ORDER_ROUTE_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        /// <summary>
        /// 添加自选线路
        /// </summary>
        /// <param name="routeDays">线路天数</param>
        /// <param name="scenicIds">景点ID</param>
        /// <param name="expire">线路说明</param>
        /// <returns></returns>
        public string[] AddOrderRouteInfo(string routeDays, string scenicIds, string expire, string routeName, string country_id, string country_name, string city_id, string city_name, string destnation_id, string destnation_name)
        {
            int days = 0;
            if (string.IsNullOrWhiteSpace(routeDays)
                || !int.TryParse(routeDays, out days)
                || days <= 0)
            {
                return new string[] { "False", "线路日程错误" };
            }

            if (string.IsNullOrWhiteSpace(scenicIds))
            {
                return new string[] { "False", "请选择至少一个景点" };
            }

            string[] scenicIdArray = scenicIds.Split(',');

            DataSet ds = new CDmDjkOrderRoute();
            DataRow dr = ds.Tables[0].NewRow();
            string routeid = this.DataObj.NewID();
            dr["ORDER_ROUTE_ID"] = routeid;
            dr["ORDER_ROUTE_NME"] = routeName;

            dr["COUNTRY_ID"] = country_id;
            dr["COUNTRY_NME"] = country_name;
            dr["CITY_ID"] = city_id;
            dr["CITY_NME"] = city_name;
            dr["DESTINATION_ID"] = destnation_id;
            dr["DESTINATION_NME"] = destnation_name;

            dr["ORDER_ROUTE_DAYS"] = days;
            dr["ROUTE_SCENIC_NUM"] = scenicIdArray.Length;
            dr["ROUTE_ORDER_EXPIRE"] = expire;
            dr["ROUTE_IS_SHOW"] = 1;
            dr["ROUTE_INFO_STATUS"] = 0;
            dr["ROUTE_IS_VALID"] = 1;
            dr["CREATE_ID"] = msBsc.SvRef.UserInfo.Operator;
            dr["CREATE_DT"] = this.dtCurrDateTime;

            ds.Tables[0].Rows.Add(dr);

            this.Update(ds);

            using (CBizDjkOrderRouteDetail objCBizDjkOrderRouteDetail = new CBizDjkOrderRouteDetail())
            {
                string[] result = objCBizDjkOrderRouteDetail.AddOrderRouteDetail(scenicIds, "1", dr["ORDER_ROUTE_ID"].ToString());

                if (result[0] == "True")
                {
                    return new string[] { "True", routeid };
                }
                else
                {
                    this.DataObj.ExecuteBySQL(@"UPDATE " + this.TableName + @" SET ROUTE_IS_VALID=0 WHERE ORDER_ROUTE_ID=" + dr["ORDER_ROUTE_ID"].ToString() + "");

                    return new string[] { "False", result[1] };
                }
            }
        }

        /// <summary>
        /// 获取用户自选路线
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-29
        public DataSet GetOrderRoute(string userid)
        {
            if (string.IsNullOrEmpty(userid))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("userid", ParameterDirection.Input, EDbType.String, 36, userid));

            return this.DataObj.QueryBySQL(@"SELECT TOP 1 * FROM DJK_ORDER_ROUTE 
WHERE ROUTE_IS_SHOW=1 
AND ORDER_ROUTE_NME='' 
AND CREATE_ID=@userid
ORDER BY CREATE_DT DESC ", parmCollection);

        }

        /// <summary>
        /// 获取 已预定的 推荐路线
        /// </summary>
        /// <param name="userid">定制人</param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-29
        public DataSet GetOrderRouteList(string userid)
        {
            if (string.IsNullOrEmpty(userid))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("userid", ParameterDirection.Input, EDbType.String, 36, userid));
            parmCollection.Add(new CDbParm("datatime", ParameterDirection.Input, EDbType.String, 36, this.dtCurrDateTime.ToString("yyyy-MM-dd")));

            return this.DataObj.QueryBySQL(@"SELECT * FROM DJK_ORDER_ROUTE 
WHERE ROUTE_IS_SHOW=1 
AND ORDER_ROUTE_NME!='' 
AND CREATE_DT>@datatime
AND CREATE_ID=@userid ", parmCollection);
        }

    }
}
